package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dao.DishDao;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.IDishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishDao, Dish> implements IDishService {

    @Autowired
    private DishFlavorServiceImpl dishFlavorService;


    @Autowired
    private SetmealServiceImpl setmealService;

    /**
     * 新增菜品同时保存对应的口味
     *
     * @param dishDto
     */
    @Override
    @Transactional//开启事务
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品基本信息到菜品表
        this.save(dishDto);
        //获取当前菜品id
        Long dishid = dishDto.getId();
        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.stream().map((item) -> {
            //将菜品id赋值给口味表中相对应菜品口味的id
            item.setDishId(dishid);
            return item;

        }).collect(Collectors.toList());


        //保存菜品口味到口味表 -dishflavor
        dishFlavorService.saveBatch(dishDto.getFlavors());
    }

    /**
     * 根据id查询菜品信息及口味信息
     *
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息  dish
        Dish dish = this.getById(id);
        //创建dishDto对象
        DishDto dishDto = new DishDto();
        //拷贝基本菜品信息,存入dishDto中
        BeanUtils.copyProperties(dish, dishDto);
        //查询菜品口味信息  dishFlavor
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(flavors);

        return dishDto;
    }

    /**
     * 跟新菜品信息及口味信息
     *
     * @param dishDto
     */
    @Transactional
    @Override
    public void updateWithFlavor(DishDto dishDto) {
        //1.跟新dish表基本信息
        this.updateById(dishDto);
        //2.跟新dishFlavor表口味信
        //2.1删除当前菜品口味信息--dishFlavor--delete方法
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(queryWrapper);
        //2.2从新添加当前菜品口味信息--dishFlavor--insert方法
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            //将菜品id赋值给口味表中相对应菜品口味的id
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Setmeal> dishQueryWrapper = new LambdaQueryWrapper<>();
     /* // 添加查询条件,根据分类id查询
        dishQueryWrapper.eq(Setmeal::getCategoryId, id);
        int count = dishService.count(dishQueryWrapper);
        //1.查询分类是否关联菜品
        if (count > 0) {
            //已关联菜品，抛出业务异常
            throw new CustomException("当前分类下关联了菜品，不能直接删除");
        }
        //2.查询当前分类时都关联套餐，如果已关联，抛出一个业务异常
        LambdaQueryWrapper<Setmeal> setmealQueryWrapper = new LambdaQueryWrapper<>();
        setmealQueryWrapper.eq(Setmeal::getCategoryId, id);
        int count1 = setmealService.count(setmealQueryWrapper);
        if (count1 > 0) {
            //已关联套餐，抛出业务异常
            throw new CustomException("当前分类下关联了套餐，不能直接删除");
        }
        //3.正常的删除方法*/
        super.removeById(id);
    }
}
